package com.example.secondhandspringboot.configuration;

import com.example.secondhandspringboot.interceptor.GetGoodsInterceptor;
import com.example.secondhandspringboot.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    private final LoginInterceptor loginInterceptor;
    private final GetGoodsInterceptor getGoodsInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/user/userInfo")
                .addPathPatterns("/user/perfectUserInfo")
                .addPathPatterns("/comments/**")
                .addPathPatterns("/goods/**")
                .addPathPatterns("/requiredGoods/**")
                .addPathPatterns("/aiTalk/**")
                .addPathPatterns("/chatMessages/**")
                .addPathPatterns("/file/**")
                .addPathPatterns("/contacts/**")
                .addPathPatterns("/userBehavior/**")
                .excludePathPatterns("/requiredGoods/getRequiredGoods")
                .excludePathPatterns("/goods/getGoods")
                .excludePathPatterns("/goods/recommendGoods")
                .excludePathPatterns("/goods/getGoodsHotTop");
        //无需登录即可查看商品
        registry.addInterceptor(getGoodsInterceptor)
                .addPathPatterns("/goods/getGoods")
                .addPathPatterns("/goods/recommendGoods")
                .addPathPatterns("/goods/getGoodsHotTop")
                .addPathPatterns("/requiredGoods/getRequiredGoods");
    }
}
